package com.example.beson.androiddemo.utils;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beson.androiddemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 史国院 on 2017/4/13.
 * Desc ******
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardHolder>{
    private List<String> list=new ArrayList<>();
    private Context mContext;
    private int viewFooter;
    private static final int TYPE_NORMAL = 1000;
    private static final int TYPE_FOOTER = 1002;
    public CardAdapter(List<String> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
    }

    public void changeData(List<String> list){
        this.list = list;
        notifyDataSetChanged();
    }
    public void addFooterView(int footerView) {
        this.viewFooter = footerView;
        notifyItemInserted(getItemCount() - 1);
    }
    public void setFooterVisible(int visible) {
        footerView.setVisibility(visible);
    }
    private View footerView;
    @Override
    public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            footerView = LayoutInflater.from(mContext).inflate(viewFooter, parent, false);
            CardHolder reViewholder = new CardHolder(footerView);
            return reViewholder;
        }else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.card_item_layout, parent, false);
            CardHolder reViewholder = new CardHolder(view);
            return reViewholder;
        }
    }

    @Override
    public void onBindViewHolder(final CardHolder holder, int position) {
        if(!(viewFooter != 0 && position == getItemCount()-1)){
            holder.cardText.setText(list.get(position));
            if(mOnItemClickListener!=null){
                holder.cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position=holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.cardView,position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isFooterView(position)) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }
    private boolean isFooterView(int position) {
        return viewFooter != 0 && position == getItemCount() - 1;
    }
    @Override
    public int getItemCount() {
        int count = (list == null ? 0 : list.size());
        if (viewFooter != 0) {
            count++;
        }
        return count;
    }
    static class CardHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView cardImage;
        TextView cardText;

        public CardHolder(View itemView) {
            super(itemView);
            cardView=(CardView)itemView;
            cardImage= (ImageView) itemView.findViewById(R.id.card_iamge);
            cardText= (TextView) itemView.findViewById(R.id.card_text);
        }
    }
    public interface onItemClickListener{
        void onItemClick(View view,int position);
    }
    private onItemClickListener mOnItemClickListener;
    public void setOnItemClickLinstener(onItemClickListener mOnItemClickListener){
        this.mOnItemClickListener=mOnItemClickListener;
    }

    /**
     * 处理GridLayoutManager
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == TYPE_NORMAL
                            ? 1 : gridManager.getSpanCount();
                }
            });
        }
    }

    /**
     * 处理StaggeredGridLayoutManager
     */
    @Override
    public void onViewAttachedToWindow(CardHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(holder.getLayoutPosition() == 0);
        }
    }
}
